package com.atguigu.tingshu.search.receiver;

import com.atguigu.tingshu.common.constant.KafkaConstant;
import com.atguigu.tingshu.search.service.SearchService;
import jakarta.annotation.Resource;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

/**
 * ClassName: SearchReceiver
 * Package: com.atguigu.tingshu.search.receiver
 * Description:
 *
 * @Author:
 * @Create: 2025/7/30 - 13:12
 * @Version: v1.0
 */

@Component
public class SearchReceiver {


    @Resource
    private SearchService searchService;

    /**
     * 监听专辑上架
     *
     * @param consumerRecord
     */
    @KafkaListener(topics = KafkaConstant.QUEUE_ALBUM_UPPER)
    public void albumUpper(ConsumerRecord<String, String> consumerRecord) {
        String albumId = consumerRecord.value();
        //判断专辑id不为空就上架
        if (StringUtils.hasText(albumId)) {
            searchService.upperAlbum(Long.valueOf(albumId));
        }
    }


    /**
     * 监听专辑下架
     *
     * @param consumerRecord
     */
    @KafkaListener(topics = KafkaConstant.QUEUE_ALBUM_LOWER)
    public void lowerGoods(ConsumerRecord<String, String> consumerRecord) {
        //  获取到发送的消息
        String albumId = consumerRecord.value();
        if (StringUtils.hasText(albumId)) {
            searchService.lowerAlbum(Long.valueOf(albumId));
        }
    }
}
